package com.example.petshopping.ui.follows.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.ui.follows.model.Store;
import com.example.petshopping.ui.follows.model.StoreProduct;

import java.util.ArrayList;
import java.util.List;

public class StoreAdapter extends RecyclerView.Adapter<StoreAdapter.StoreViewHolder> {

    private Context context;
    private List<Store> storeList;
    private List<Store> filteredStoreList;
    private OnStoreActionListener listener;
    private boolean hasRecommendHeader = false;

    public interface OnStoreActionListener {
        void onFollowClick(Store store);
        void onEnterStoreClick(Store store);
    }

    public StoreAdapter(Context context, List<Store> storeList, OnStoreActionListener listener) {
        this.context = context;
        this.storeList = storeList;
        this.listener = listener;
        this.filteredStoreList = new ArrayList<>(storeList);
    }

    @NonNull
    @Override
    public StoreViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_store, parent, false);
        return new StoreViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull StoreViewHolder holder, int position) {
        // 检查是否需要显示推荐标题
        if (position == 1 && hasRecommendHeader) {
            holder.recommendHeader.setVisibility(View.VISIBLE);
        } else {
            holder.recommendHeader.setVisibility(View.GONE);
        }

        Store store = filteredStoreList.get(position);

        // 设置店铺基本信息
        holder.tvStoreName.setText(store.getName());
        holder.ivStoreAvatar.setImageResource(store.getAvatarResourceId());
        holder.tvStoreFollowers.setText(store.getFollowers());

        // 设置评分
        StringBuilder rating = new StringBuilder();
        for (int i = 0; i < store.getRating(); i++) {
            rating.append("★");
        }
        holder.tvStoreRating.setText(rating.toString());

        // 设置新店标签
        if (store.isNewStore()) {
            holder.tvNewStoreBadge.setVisibility(View.VISIBLE);
        } else {
            holder.tvNewStoreBadge.setVisibility(View.GONE);
        }

        // 设置关注/进店按钮
        if (store.isFollowed()) {
            holder.btnFollow.setText("取消关注");
            holder.btnFollow.setBackgroundResource(R.drawable.bg_button_gray);
            holder.btnFollow.setTextColor(context.getResources().getColor(R.color.text_gray));
        } else {
            holder.btnFollow.setText("关注");
            holder.btnFollow.setBackgroundResource(R.drawable.bg_button_purple);
            holder.btnFollow.setTextColor(context.getResources().getColor(android.R.color.white));
        }
        
        // 确定要显示哪个按钮（关注/进店）
        if (store.isFollowed() && position == 0) {
            holder.btnEnterStore.setVisibility(View.VISIBLE);
            holder.btnFollow.setVisibility(View.GONE);
        } else {
            holder.btnEnterStore.setVisibility(View.GONE);
            holder.btnFollow.setVisibility(View.VISIBLE);
        }

        // 设置店铺产品
        List<StoreProduct> products = store.getProducts();
        if (products != null && products.size() >= 4) {
            holder.ivProduct1.setImageResource(products.get(0).getImageResourceId());
            holder.tvPrice1.setText("¥" + products.get(0).getPrice());

            holder.ivProduct2.setImageResource(products.get(1).getImageResourceId());
            holder.tvPrice2.setText("¥" + products.get(1).getPrice());

            holder.ivProduct3.setImageResource(products.get(2).getImageResourceId());
            holder.tvPrice3.setText("¥" + products.get(2).getPrice());

            holder.ivProduct4.setImageResource(products.get(3).getImageResourceId());
            holder.tvPrice4.setText("¥" + products.get(3).getPrice());
        }

        // 设置按钮点击监听器
        holder.btnEnterStore.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEnterStoreClick(store);
            }
        });

        holder.btnFollow.setOnClickListener(v -> {
            if (listener != null) {
                listener.onFollowClick(store);
            }
        });
    }

    @Override
    public int getItemCount() {
        return filteredStoreList.size();
    }

    public void filterByTab(int tabIndex) {
        filteredStoreList.clear();
        hasRecommendHeader = false;

        // 筛选对应标签下的商店
        for (Store store : storeList) {
            if (store.getTabIndex() == tabIndex) {
                filteredStoreList.add(store);
                
                // 如果是第一个标签，并且有未关注的店铺，则添加推荐标题
                if (tabIndex == 0 && !store.isFollowed()) {
                    hasRecommendHeader = true;
                }
            }
        }
        
        notifyDataSetChanged();
    }

    static class StoreViewHolder extends RecyclerView.ViewHolder {
        ImageView ivStoreAvatar;
        TextView tvStoreName, tvStoreRating, tvStoreFollowers, tvNewStoreBadge;
        Button btnEnterStore, btnFollow;
        TextView recommendHeader;
        
        // 商品视图
        ImageView ivProduct1, ivProduct2, ivProduct3, ivProduct4;
        TextView tvPrice1, tvPrice2, tvPrice3, tvPrice4;

        StoreViewHolder(@NonNull View itemView) {
            super(itemView);
            
            // 店铺基本信息
            ivStoreAvatar = itemView.findViewById(R.id.ivStoreAvatar);
            tvStoreName = itemView.findViewById(R.id.tvStoreName);
            tvStoreRating = itemView.findViewById(R.id.tvStoreRating);
            tvStoreFollowers = itemView.findViewById(R.id.tvStoreFollowers);
            tvNewStoreBadge = itemView.findViewById(R.id.tv_new_store_badge);
            btnEnterStore = itemView.findViewById(R.id.btnEnterStore);
            
            // btnFollow 可能在布局中不存在，需要添加到布局中
            // 临时解决方案：可以先用 btnEnterStore 替代
            btnFollow = itemView.findViewById(R.id.btnEnterStore); // 临时使用，后续需要添加正确的按钮
            
            recommendHeader = itemView.findViewById(R.id.tv_recommend_header);
            
            // 店铺产品
            ivProduct1 = itemView.findViewById(R.id.ivProduct1);
            ivProduct2 = itemView.findViewById(R.id.ivProduct2);
            ivProduct3 = itemView.findViewById(R.id.ivProduct3);
            ivProduct4 = itemView.findViewById(R.id.ivProduct4);
            tvPrice1 = itemView.findViewById(R.id.tvProductPrice1);
            tvPrice2 = itemView.findViewById(R.id.tvProductPrice2);
            tvPrice3 = itemView.findViewById(R.id.tvProductPrice3);
            tvPrice4 = itemView.findViewById(R.id.tvProductPrice4);
        }
    }
} 